<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
use App\Goods;
use DB;
use App\User;


class OrderController extends Controller
{
   public function create(Request $request)
    {
        $data = [];
        //过滤数据
        foreach ($request->goods as $key => $value) {
            if(!empty($value['id'])){
                $data[] = $value;
            }
        }

        $order = new Order;
        //订单编号
        $order -> order_num = $this->getNewOrderNum();

        //登录用户ID
        $order -> user_id = session('id');
        //计算价格
        $order -> price = $this->getTotalPrice($data);
        //存储
        if($order->save()) {
            $res = [];
            foreach ($data as $key => $value) {
                $tmp = [];
                $tmp['order_id'] = $order->id;
                $tmp['goods_id'] = $value['id'];
                $tmp['number'] = $value['num'];
                $res[] = $tmp;
            }
            if(DB::table('order_goods')->insert($res)) {
                return redirect('/order/'.$order->order_num.'.html');
            }else{
                return back();
            }
        }
    }

    /**
     * 获取新的订单号
     */
    private function getNewOrderNum()
    {
        return time().rand(10000,99999);
    }

    /**
     * 计算总价
     */    
    private function getTotalPrice($data)
    {
        $total = 0;
        foreach ($data as $key => $value) {
            //读取商品的价格
            $goods = Goods::find($value['id']);
            //
            $total += $goods->price * $value['num'];

        }
        return $total;
    }


    /**
     * 订单的确认页面
     */
    public function confirm($order)
    {
        //读取信息
        $order = Order::where('order_num', $order)->first();
        if(empty($order)) abort(404);
        // dd($order);
        $res = $order->goods;
        
        //获取用户的信息
        $user = User::find(session('id'));
        // dd($user->address);
        return view('home.cart.confirm',['user'=>$user,'order'=>$order]);

    }


    public function doconfirm(Request $request)
    {  
        //更新订单表的信息
        $order = Order::find($request->order_id);

        $order->address_id = $request->address;

        $order->status = 1;

        if($order->save()) {
            
            $url = 'http://pay.xiaohigh.com/api/deal?to=526942358@qq.com&money=200&order_id='.$order->order_num.'&info='.'凡客诚品'.'&return_url='.'http://vancl.com/return_url';
            
            return redirect($url);

        }

    }


    public function success(Request $request)
    {
        //获取订单编号
        /**
         * http://171.com/return_url?body=%E6%96%B0%E6%AC%BE%E7%9A%84%E9%9E%8B%E5%AD%90+%E8%A1%A3%E6%9C%8D+%E7%89%9B%E4%BB%94%E8%A3%A4&buyer_email=liximing2011%40gmail.com&buyer_id=2088302613176362&exterface=create_direct_pay_by_user&is_success=T&notify_id=RqPnCoPT3K9%252Fvwbh3InZeqUOojnH7iMfv1gZ4wK69n3DFx%252Bb34Kxhp66nq2CNHYwn2eP&notify_time=2017-03-12+11%3A19%3A25&notify_type=trade_status_sync&out_trade_no=148928596125040&payment_type=1&seller_email=779498590%40qq.com&seller_id=2088221571819819&subject=171%E7%AB%99%E5%95%86%E5%93%81%E8%B4%AD%E4%B9%B0&total_fee=1.00&trade_no=2017031221001004360217383677&trade_status=TRADE_SUCCESS&sign=2414978c720213f28375d7c06e3c5b30&sign_type=MD5
         */
        $order_num = $request->out_trade_no;

        //获取订单
        $order = Order::where('order_num', $order_num)->first();

        $order->status = 2;

        if($order->save()) {
            return view('home.pay.success');
        }else{
            return view('home.pay.error');
        }
    }

}
